﻿using Microsoft.Extensions.DependencyInjection;
using Quartz;
using System;
using System.Collections.Generic;
using System.IO;

namespace Handday.Erp.JobTask
{
    /// <summary>
    /// quartz
    /// </summary>
    public static class QuartzServiceExtensions
    {
        /// <summary>
        /// quartz service
        /// </summary>
        /// <param name="services">IServiceCollection</param>
        /// <returns></returns>
        public static IServiceCollection AddQuartzService(this IServiceCollection services)
        {
            var confPathList = new List<string>
            {
                Path.Combine(AppContext.BaseDirectory,"quartz_jobs.config")
            };
            services.AddQuartz(config =>
            {
                config.Properties["quartz.scheduler.instanceName"] = "Handday Erp Transfer TaskJob Scheduler";
                config.UseDefaultThreadPool(3);
                config.UseMicrosoftDependencyInjectionJobFactory();
                config.UseXmlSchedulingConfiguration(x =>
                {
                    x.Files = confPathList.ToArray();
                    x.ScanInterval = TimeSpan.FromSeconds(5);
                    x.FailOnFileNotFound = false;
                    x.FailOnSchedulingError = true;
                });
            });

            //注册通用job
            services.AddScoped<IJob, CommonJob>();

            services.AddQuartzHostedService(options =>
            {
                options.WaitForJobsToComplete = true;
            });

            return services;
        }
    }
}
